package artibella.ui.combos
{
	import artibella.IDisposable;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import caurina.transitions.Tweener;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class ACombo extends Sprite implements IDisposable
	{
		
		protected var _header:Sprite;
		protected var _header_bg:Sprite;
		protected var _header_btn:Sprite;
		
		protected var _selectedIndice:int = -1;
		protected var _defaultText:String = "---";
		
		protected var _masque:Sprite;
		protected var _combolist:ComboList;
		
		private var _hauteurItem:int;
		private var _nbVisibleItems:int;
		
		private var _stage:Stage;
		private var _opened:Boolean = false;
		
		
		public function ACombo( ) 
		{
			_init();
		}
		
		private function _init():void
		{
			_header = new Sprite();
			_masque = new Sprite();
			_combolist = new ComboList( this );
		}
		
		// - - -
		
		public function get selectedIndice():int 
		{
			return _selectedIndice;
		}
		
		public function get opened():Boolean 
		{
			return _opened;
		}
		
		
		// - - -
		
		public function setHeaderBackground( c:Sprite ):void
		{
			_header_bg = c;
		}
		
		public function setButton( c:Sprite ):void
		{
			_header_btn = c;
		}

		public function setDefaultSelection( i:int, str:String = null ):void
		{
			_selectedIndice = i;
			
			if( str == null ) {
				_defaultText = "---";
			} else {
				_defaultText = str;
			}
		}
		
		public function selectItem( e:MouseEvent ):void
		{
			var item:AComboListItem = e.target as AComboListItem;
			
			_selectedIndice = item.indice;

			_doClick( null );
		}
		
		// - - -
		
		public function addItem( c:AComboListItem ):void
		{
			_hauteurItem = c.height;
			_combolist.addItem( c );
		}
		
		public function finalize( s:Stage, nb:int = 5 ):void 
		{
			_stage = s;
			_nbVisibleItems = nb;
			
			if ( _combolist.nbItems == 0 ) {
				throw new Error( "the combolist is empty" );
			} else {
				if( _combolist.nbItems < _nbVisibleItems ) _nbVisibleItems = _combolist.nbItems;
			}
			
			if ( _header_bg == null ) setHeaderBackground( new ComboHeaderBackground() );
			if ( _header_btn == null ) setButton( new ComboButton() );
			
			var largeur:int = _header_bg.width;
			var hauteur:int = _nbVisibleItems * _hauteurItem;
			
			_setHeader( largeur, hauteur );
			_setList( largeur, hauteur );
			_setMasque( largeur, hauteur );
			
			activate( true );
		}
		
		// - - -
		
		private function _setHeader( w:int, h:int ):void
		{
			_header_bg.x = 0;
			_header_bg.y = 0;
			_header.addChild( _header_bg );
			
			_header_btn.x = w - _header_btn.width;
			_header_btn.y = 0;
			_header.addChild( _header_btn );
			
			_setHeaderText();
			
			addChild( _header );
		}
		
		protected function _setHeaderText():void
		{
			// override this
		}
		
		protected function _setList( w:int, h:int ):void
		{
			_combolist.finalize( w, h );
			
			_combolist.x = 0;
			_combolist.y = - _combolist.height + _header_bg.height;
			_combolist.alpha = .95;
			
			addChild( _combolist );
		}
		
		protected function _setMasque( w:int, h:int ):void
		{
			with ( _masque.graphics ) {
				beginFill( 0xFF0000 );
				drawRect( 0, 0, w, h );
				endFill( );
			}
			
			_masque.x = 0;
			_masque.y = _header_bg.height + 1;
			
			addChild( _masque );
			
			_combolist.mask = _masque;
		}
		
		// - - -
		
		public function activate( b:Boolean ):void
		{
			if ( b ) {
				_header.buttonMode = true;
				_header.mouseChildren = false;
				_header.addEventListener( MouseEvent.CLICK, _doClick );
			} else {
				_header.removeEventListener( MouseEvent.CLICK, _doClick );
			}
		}
		
		private function _doClick( e:MouseEvent = null ):void
		{
			if ( _opened ) {
				
				Tweener.addTween( _combolist, { y: - _masque.height + _header_bg.height, time:.8 } );
				
			} else {
				
				Tweener.addTween( _combolist, { y: _header_bg.height + 1, time:.8 } );
				
				dispatchEvent( new ComboEvent( ComboEvent.OPENED, -1, _defaultText ) );
			}
			
			_opened = !_opened;
			
			if ( _header_btn is IStateSetable ) {
				_header_btn[ "setState" ]( _opened );
			} 
		}
		
		public function doClose( e:MouseEvent = null ):void
		{
			if ( _opened ) {
				

				Tweener.addTween( _combolist, { y: - _masque.height + _header_bg.height, time:.8 } );
				
				_opened = false;
				
				if ( _header_btn is IStateSetable ) {
					_header_btn[ "setState" ]( _opened );
				} 
			}
		}
		
		// ----- IDisposable
		
		public function dispose():void
		{
			activate( false );

		}
	}
}